package com.eshop.tools.service.impl;

import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.eshop.exception.BadRequestException;
import com.eshop.tools.domain.vo.TradeVo;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.tools.service.AlipayConfigService;
import com.eshop.tools.domain.AlipayConfig;
import com.eshop.tools.service.mapper.AlipayConfigMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class AlipayConfigServiceImpl extends BaseServiceImpl<AlipayConfigMapper, AlipayConfig> implements AlipayConfigService
{
    public String toPayAsPc(final AlipayConfig alipay, final TradeVo trade) throws Exception {
        if (alipay.getId() == null) {
            throw new BadRequestException("请先添加相应配置，再操作");
        }
        final AlipayClient alipayClient = (AlipayClient)new DefaultAlipayClient(alipay.getGatewayUrl(), alipay.getAppId(), alipay.getPrivateKey(), alipay.getFormat(), alipay.getCharset(), alipay.getPublicKey(), alipay.getSignType());
        final AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setReturnUrl(alipay.getReturnUrl());
        request.setNotifyUrl(alipay.getNotifyUrl());
        request.setBizContent("{    \"out_trade_no\":\"" + trade.getOutTradeNo() + "\",    \"product_code\":\"FAST_INSTANT_TRADE_PAY\",    \"total_amount\":" + trade.getTotalAmount() + ",    \"subject\":\"" + trade.getSubject() + "\",    \"body\":\"" + trade.getBody() + "\",    \"extend_params\":{    \"sys_service_provider_id\":\"" + alipay.getSysServiceProviderId() + "\"    }  }");
        return ((AlipayTradePagePayResponse)alipayClient.pageExecute((AlipayRequest)request, "GET")).getBody();
    }
    
    public String toPayAsWeb(final AlipayConfig alipay, final TradeVo trade) throws Exception {
        if (alipay.getId() == null) {
            throw new BadRequestException("请先添加相应配置，再操作");
        }
        final AlipayClient alipayClient = (AlipayClient)new DefaultAlipayClient(alipay.getGatewayUrl(), alipay.getAppId(), alipay.getPrivateKey(), alipay.getFormat(), alipay.getCharset(), alipay.getPublicKey(), alipay.getSignType());
        final double money = Double.parseDouble(trade.getTotalAmount());
        final double maxMoney = 5000.0;
        if (money <= 0.0 || money >= maxMoney) {
            throw new BadRequestException("测试金额过大");
        }
        final AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setReturnUrl(alipay.getReturnUrl());
        request.setNotifyUrl(alipay.getNotifyUrl());
        request.setBizContent("{    \"out_trade_no\":\"" + trade.getOutTradeNo() + "\",    \"product_code\":\"FAST_INSTANT_TRADE_PAY\",    \"total_amount\":" + trade.getTotalAmount() + ",    \"subject\":\"" + trade.getSubject() + "\",    \"body\":\"" + trade.getBody() + "\",    \"extend_params\":{    \"sys_service_provider_id\":\"" + alipay.getSysServiceProviderId() + "\"    }  }");
        return ((AlipayTradeWapPayResponse)alipayClient.pageExecute((AlipayRequest)request, "GET")).getBody();
    }
    
    public AlipayConfig find() {
        final AlipayConfig alipayConfig = this.list().get(0);
        return alipayConfig;
    }
    
    @Transactional(rollbackFor = { Exception.class })
    public void update(final AlipayConfig alipayConfig) {
        this.save(alipayConfig);
    }
}
